package com.db.compare.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.db.compare.exp.RbacMenuTemplateExp;
import com.db.compare.model.RbacMenuTemplate;
import com.db.compare.mapper.RbacMenuTemplateMapper;
import com.db.compare.service.RbacMenuTemplateService;
import com.db.compare.vo.RbacMenuTemplateVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class RbacMenuTemplateServiceImpl implements RbacMenuTemplateService {
    @Autowired
    private RbacMenuTemplateMapper rbacmenutemplateMapper;
    @Override
    public PageInfo<RbacMenuTemplate> pageInfo(RbacMenuTemplateVo rbacmenutemplateVo) {
        PageHelper.startPage(rbacmenutemplateVo.getPageNum(),rbacmenutemplateVo.getPageSize());
        List<RbacMenuTemplate> rbacmenutemplateList = rbacmenutemplateMapper.findList(rbacmenutemplateVo);
        PageInfo<RbacMenuTemplate> pageInfo = new PageInfo<>(rbacmenutemplateList);
        return pageInfo;
    }

    @Override
    public void download(String ids, HttpServletResponse response) throws Exception {
        String fileName = "菜单模板管理";
        try {
            ServletOutputStream outputStream=Appcontext.downloadResponse(response,fileName);
            if(StringUtils.isBlank(ids)){
                EasyExcel.write(outputStream, RbacMenuTemplateExp.class).sheet(fileName).doWrite(null);
            }else{
                String[] arrays = ids.split(",");
                List<RbacMenuTemplate> rbacmenutemplateList = rbacmenutemplateMapper.findByIds(arrays);
                List<RbacMenuTemplateExp> rbacmenutemplateExpList = new ArrayList<>();
                for(RbacMenuTemplate rbacmenutemplate:rbacmenutemplateList){
                    RbacMenuTemplateExp rbacmenutemplateExp = new RbacMenuTemplateExp();
                    BeanUtils.copyProperties(rbacmenutemplate,rbacmenutemplateExp);
                    rbacmenutemplateExpList.add(rbacmenutemplateExp);
                }
                EasyExcel.write(outputStream, RbacMenuTemplateExp.class).sheet(fileName).doWrite(rbacmenutemplateExpList);
            }
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean upload(MultipartFile file) {
        ImportParams importParams = new ImportParams();
        try {
            ExcelImportResult<RbacMenuTemplateExp> result = ExcelImportUtil.importExcelMore(file.getInputStream(), RbacMenuTemplateExp.class,
                    importParams);
            List<RbacMenuTemplateExp> rbacmenutemplateExps = result.getList();
            for (RbacMenuTemplateExp rbacmenutemplateExp : rbacmenutemplateExps) {
                RbacMenuTemplate rbacmenutemplate = new RbacMenuTemplate();
                BeanUtils.copyProperties(rbacmenutemplate,rbacmenutemplateExp);
                int i = rbacmenutemplateMapper.insert(rbacmenutemplate);
                if(i < 1){
                    return false;
                }
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
